/*
 * Created by JFormDesigner on Mon Dec 16 21:15:25 CST 2024
 */

package com.yckj.jformdesinger;

import com.yckj.finance.dao.ProductDao;
import com.yckj.finance.entity.Product;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;

/**
 * @author wzj
 */
public class ProductList extends JFrame {
    public ProductList() {
        initComponents();
    }

    public static void main(String[] args) {
        new ProductList().setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        productTable = new JTable();
        deleteProductBtn = new JButton();
        updateProductBtn = new JButton();

        //======== this ========
        setTitle("\u5b9e\u7269\u5206\u7c7b\u663e\u793a");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {

            //---- productTable ----
            productTable.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u7c7b\u522b\u7f16\u53f7", "\u7c7b\u522b\u540d\u79f0", "\u5907\u6ce8"
                }
            ));
            scrollPane1.setViewportView(productTable);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(40, 90, scrollPane1.getPreferredSize().width, 235);

        //---- deleteProductBtn ----
        deleteProductBtn.setText("\u5220\u9664\u5b9e\u7269\u7c7b\u522b");
        contentPane.add(deleteProductBtn);
        deleteProductBtn.setBounds(new Rectangle(new Point(125, 40), deleteProductBtn.getPreferredSize()));

        //---- updateProductBtn ----
        updateProductBtn.setText("\u4fee\u6539\u5b9e\u7269\u7c7b\u522b");
        contentPane.add(updateProductBtn);
        updateProductBtn.setBounds(new Rectangle(new Point(280, 40), updateProductBtn.getPreferredSize()));

        contentPane.setPreferredSize(new Dimension(540, 405));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        selectProduct();
        deleteProductBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取选中行
                int row = productTable.getSelectedRow();
                if (row == -1) {
                    JOptionPane.showMessageDialog(null, "请选择一条记录", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                int result = JOptionPane.showConfirmDialog(null, "确认删除该分类吗？", "提示",
                        JOptionPane.YES_NO_OPTION);
                System.out.println(result);
                if (result == 0) {
                    int id = Integer.valueOf(productTable.getValueAt(row, 0).toString());
                    int flag = new ProductDao().deleteProduct(id);
                    if (flag == 1) {
                        JOptionPane.showMessageDialog(null, "删除成功!");
                        // 再次查询所有数据，填充到表格
                        selectProduct();
                    } else {
                        JOptionPane.showMessageDialog(null, "操作失败", "系统提示", JOptionPane.WARNING_MESSAGE);

                    }
                }
                return;
            }
        });
    }
    // 填充表格数据
    public void selectProduct() {
        // 调用ProductDao里的方法
        List<Product> list = new ProductDao().selectProduct();
        //得到数据表格
        DefaultTableModel tableModel = (DefaultTableModel) productTable.getModel();
        tableModel.setRowCount(0);// 清除原有行
        // 遍历集合list，把数据填充到表格里
        for (Product item : list) {
            // 这个数组放表里的一条数据
            String[] arr = new String[3];
            arr[0] = item.getProductid() + "";
            arr[1] = item.getProducttype();
            arr[2] = item.getRemark();
            // 添加数据到表格
            tableModel.addRow(arr);
        }
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JTable productTable;
    private JButton deleteProductBtn;
    private JButton updateProductBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
